package com.shike.hq.controller;

import com.shike.hq.entity.*;
import com.shike.hq.service.GroupService;
import com.shike.hq.service.LabelService;
import com.shike.hq.service.UnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;

/**
 * The type Labels controller.
 */
@Controller
@RequestMapping("/label")
public class LabelsController {

    private static final String REDIRECT = "redirect:/label/labels";
    @Autowired
    LabelService labelService;
    @Autowired
    UnitService unitService;
    @Autowired
    GroupService groupService;

    /**
     *标签页面路径映射
     *
     * @param model model类型
     * @param session session类型
     * @return 页面路径映射字段
     */
    @GetMapping("/labels")
    public String label(Model model, HttpSession session) {
        Admin admin =(Admin) session.getAttribute("admin");
        long areaId = admin.getAreaId();
        ArrayList<Label> labels = labelService.selectAllLabelByAreaId(areaId);
        if (labels.size() > 0) {
            //找到标签信息
            model.addAttribute("labels", labels);
        } else {
            model.addAttribute("labels", "null");
        }
        ArrayList<Group> groups = groupService.selectAllGroupByAreaId(areaId);
        model.addAttribute("groups", groups);
        ArrayList<Unit> units = unitService.selectAllUnitByAreaId(areaId);
        model.addAttribute("units", units);
        return "label/labels";
    }

    /**
     *单位页面路径映射
     *
     * @param model model类型
     * @param session session类型
     * @return 页面路径映射字段
     */
    @GetMapping("/label/unit")
    public String getUnit(Model model, HttpSession session) {
        Admin admin =(Admin) session.getAttribute("admin");
        long areaId = admin.getAreaId();
        ArrayList<Unit> units = unitService.selectAllUnitByAreaId(areaId);
        if (units.size() > 0) {
            //找到单位信息
            model.addAttribute("units", units);
        } else {
            model.addAttribute("units", "Oops……没有数据呀~");
        }
        return "label/labels";
    }

    /**
     *巡检组页面路径映射
     *
     * @param model model类型
     * @param session session类型
     * @return 页面路径映射字段
     */
    @GetMapping("/label/group")
    public String getGroup(Model model, HttpSession session) {
        Admin admin =(Admin) session.getAttribute("admin");
        long areaId = admin.getAreaId();
        ArrayList<Group> groups = groupService.selectAllGroupByAreaId(areaId);
        if (groups.size() > 0) {
            //找到巡检组信息
            model.addAttribute("groups", groups);
        } else {
            model.addAttribute("groups", "Oops……没有数据呀~");
        }
        return "label/labels";
    }

    /**
     * 编辑标签信息
     *
     * @param label  编辑后的标签信息
     * @param attributes model类型
     * @return 页面路径字符串
     */
    @PostMapping("/label/update")
    public String updateLabel(Label label,RedirectAttributes attributes){
        try {
            labelService.updateById(label);
            attributes.addFlashAttribute("msg", "编辑成功！");
        } catch (Exception e){
            attributes.addFlashAttribute("msg", "编辑失败！");
        }
        return REDIRECT;
    }

    /**
     * 删除标签信息
     *
     * @param id    要删除的标签id
     * @param attributes model类型
     * @return 页面路径字符串
     */
    @GetMapping("/label/delete")
    public String deleteLabel(@RequestParam("id") long id, RedirectAttributes attributes) {
        try {
            labelService.removeById(id);
            attributes.addFlashAttribute("msg", "删除成功！");
        } catch (Exception e){
            attributes.addFlashAttribute("msg", "删除失败！");
        }
        return REDIRECT;
    }

    /**
     * 删除多条标签信息
     *
     * @param idList 标签id列表
     * @param attributes  model类型
     * @return 页面路径字符串
     */
    @GetMapping("/label/deleteLabels")
    public String deleteLabels(@RequestParam("idList") ArrayList<Long> idList,
                              RedirectAttributes attributes) {
        try {
            labelService.removeByIds(idList);
            attributes.addFlashAttribute("msg", "删除成功！");
        }catch (Exception e){
            attributes.addFlashAttribute("msg", "删除失败！");
        }
        return REDIRECT;
    }

    /**
     * 添加标签信息
     *
     * @param label   添加的标签信息
     * @param attributes   model类型
     * @return 页面路径字符串
     */
    @PostMapping("/label/add")
    public String addLabel(Label label, RedirectAttributes attributes) {
        label.setCreateTime(new Timestamp(System.currentTimeMillis()));
        label.setBugTime(new Timestamp(System.currentTimeMillis()));
        label.setExpiryTime(12);
        //label.setBugTime(buyTime);  //buyTime需要转化成Timestamp类型
        //label.setExpiryTime(expiryTime);  //expiryTime需要转化成Timestamp类型
        //label.setCreateTime(createTime);  //createTime需要转化成Timestamp类型
        try {
            labelService.save(label);
            attributes.addFlashAttribute("msg", "添加成功！");
        } catch (Exception e){
            attributes.addFlashAttribute("msg", "添加失败！唯一码冲突！");
        }
        return REDIRECT;
    }

    /**
     * 指定巡检组进行页面映射.
     *
     * @param model model类型
     * @return 页面路径字符串 string
     */
    @PostMapping("/label/showRefresh")
    public String showRefresh(Model model, HttpSession session, @RequestParam("ParentId") long ParentId) {
        ArrayList<Label> list = labelService.selectLabelsByGroupId(ParentId);
        model.addAttribute("labels", list);
        Admin admin =(Admin) session.getAttribute("admin");
        long areaId = admin.getAreaId();
        ArrayList<Group> groups = groupService.selectAllGroupByAreaId(areaId);
        model.addAttribute("groups", groups);
        ArrayList<Unit> units = unitService.selectAllUnitByAreaId(areaId);
        model.addAttribute("units", units);
        return "/label/labels::showRefresh";
    }
}
